package test5;

import java.io.IOException;
import java.net.*;
import java.util.Scanner;

public class UDPSendder {

    public static void main(String[] args) {
        //声明数据报
        DatagramPacket dp = null;
        //声明数据包套接字
        DatagramSocket ds = null;
        Scanner input = new Scanner(System.in);
        String mess = null;
        byte [] buf = new byte[1024];
        byte[] data;
        try {
            ds = new DatagramSocket(8989);
            while (true){
                mess = input.nextLine();
                data = mess.getBytes();
                dp = new DatagramPacket(data,data.length,
                        InetAddress.getByName("localhost"),8888);
                ds.send(dp);
                InetAddress ia = dp.getAddress();
                dp = new DatagramPacket(buf,buf.length);
                ds.receive(dp);
                mess = new String(dp.getData(),0,dp.getLength());
                System.out.println(ia.getHostAddress() + "说:" + mess);
            }
        } catch (SocketException e) {
            e.printStackTrace();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            ds.close();
        }


    }


}
